<template>
    <div class="updateTask">
        <div class="input-box">
            <div class="input-label">具体工作内容</div>
            <div class="input-fill">
                <van-field style="background: rgba(242, 243, 245, 1)" disabled v-model="form.workContent" rows="2"
                    autosize type="textarea" placeholder="请填写" />
                <!-- <input type="text" v-model="form.workContent" disabled placeholder="请填写"> -->
                <!-- <el-input v-model="input" placeholder="请填写" /> -->
            </div>
        </div>
        <div class="input-box">
            <div class="input-label">督办意见</div>
            <div class="input-fill">
                <van-field style="background: rgba(242, 243, 245, 1)" v-model="form.superviseComment" rows="2"
                    autosize type="textarea" placeholder="请填写" />
                <!-- <input type="text" v-model="form.superviseComment" placeholder="请填写"> -->
                <!-- <el-input v-model="input" placeholder="请填写" /> -->
            </div>
        </div>
        <div class="btn-list">
            <div class="cancel btn btnPointer" @click="router.go(-1)">取消</div>
            <div class="confirm btn btnPointer" @click="onSubmit(true)">暂存</div>
            <div class="confirm btn btnPointer" @click="onSubmit(false)">提交</div>
            <!-- <div class="confirm btn btnPointer" @click="onSubmit">提交</div> -->
        </div>
    </div>
</template>

<script setup>
import { workUpdate, workAdd } from "@/service/yearWork"
const route = useRoute()
const router = useRouter()
const form = reactive({})


const cgItem = ref({})
const isFirst = ref(true)
function onSubmit() {
    let params = {
        "workYear": form.workYear ?? "",
        "businessType": form.businessType ?? "",
        "mainBusinessUnitOrgCode": form.mainBusinessUnitOrgCode ?? "",
        "businessUnitOrgCodes": form.businessUnitOrgCodes?.length > 0 ? form.businessUnitOrgCodes.join(',') : "",
        "workLeader": form.workLeader?.length > 0 ? form.workLeader.join(',') : "",
        "workStatus": form.workStatus ?? "",
        "workContent": form.workContent ?? "",
        "annualPlan": form.annualPlan ?? "",
        "workProgress": form.workProgress ?? "",
        "workStatement": form.workStatement ?? "",
        "workNote": form.workNote ?? "",
        "workCategory": form.workCategory?.length > 0 ? form.workCategory.join(',') : "",
        "workBigType": form.workBigType ?? "",
        "superviseComment": form.superviseComment ?? ""
    }
    const FormType = route.query.type
    let reqParams = {
        requestType: FormType == 'edit' ? '修改' : '新增',
        // workProgressObjectId: opType.value == 'edit' ? form.objectId : '',
        changeContentBefore: FormType == 'edit' ? changeBefore.value : null,
        changeContentAfter: params,
    }
    // 主要责任单位名称
    let mainBusinessUnit = orgList.value.find(item => {
        return item.code == form.mainBusinessUnitOrgCode
    })
    reqParams.mainBusinessUnit = mainBusinessUnit.name
    // 主要责任orgCode
    reqParams.mainBusinessUnitOrgCode = mainBusinessUnit.code
    // 相关责任单位orgCode
    reqParams.businessUnitOrgCodes = form.businessUnitOrgCodes.join(',')
    // 分管领导的
    reqParams.managerLeaderIds = params.workLeader
    if (isDraft) {
        let draftParams = {
            draftContent: params
        }
        //第一次点击暂存的时候其实走的是新增接口add，弹框不关闭，然后之后的操作都是走update接口
        let reqFnc = draftAdd
        // 第二次走更新接口
        if (!isFirst.value) {
            reqFnc = draftUpdate
            // 当表单是编辑的时候才会有源数据的关联id
            draftParams.objectId = cgItem.value.objectId
        }
        // 如果是修改的情况，则就是要加上关联元数据的id
        if (FormType == 'edit') {
            draftParams.relatedWorkId = form.objectId
        }
        reqFnc(draftParams).then(res => {
            if (res.code == 0) {
                // 记录这次草稿的数据
                if (isFirst.value) {
                    cgItem.value = res.data
                }
                // ElMessage.success('暂存成功！')
                showToast("暂存成功！");
                isFirst.value = false
            }
        })
    } else {
        let draftParams = {
            pageNo: 1,
            pageSize: -1,
            deleteFlag: 0,
            applicantId: userStore.userInfo.userId,
            draftStatus: "草稿",
            objectId: cgItem.value.objectId
        }
        draftPage(draftParams).then(res => {
            if (res.code == 0) {
                if (res.data.records.length > 0) {
                    reqParams.draftObjectId = res.data.records[0].objectId
                }
                // 提交审核申请
                workAdd(reqParams).then(res => {
                    if (res.code == 0) {
                        showToast("提交审核成功，请等待审核");
                        router.go(-1)
                    } else {
                        showToast(res.msg);
                        // ElMessage.error(res.msg)
                        // emit('success')
                    }
                })
            }
        })

    }
}

const changeBefore = ref({})
onMounted(() => {
    if (route.query.type == 'edit') {
        let editData = JSON.parse(route.query.rowData)
        // 分管领导
        let ldArr = []
        ldColumns.value.forEach((item) => {
            if (editData.workLeader.includes(item.value)) {
                ldArr.push(item.text)
            }

        })
        // 分管领导的数组
        editData.workLeader = editData.workLeader.split(',')
        editData.workLeaderName = ldArr
        // 主要责任单位
        zrColumns.value.forEach((item) => {
            if (editData.mainBusinessUnitOrgCode = item.value) {
                editData.mainBusinessUnitOrgName = item.text
            }
        })
        // 相关责任单位
        let xgZrArr = []
        zrColumns.value.forEach((item) => {
            if (editData.businessUnitOrgCodes.split(',').includes(item.value)) {
                xgZrArr = item.text
            }
        })
        // 相关责任单位的数组
        editData.businessUnitOrgCodes = editData.businessUnitOrgCodes.split(',')
        editData.businessUnitName = xgZrArr

        // 任务类别
        editData.workCategory = editData.workCategory.split(',')
        Object.assign(form, editData)
        changeBefore.value = editData
    }
    // form.workProgress = ""
})


</script>
<style></style>
<style lang="scss" scoped>
.updateTask {
    color: rgba(51, 51, 51, 1);
    padding: 0 26px;
    box-sizing: border-box;
    overflow-y: scroll;
    height: calc(100vh - 92px - 10px);
    margin-top: 10px;
    border-radius: 30px 30px 0px 0px;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px -8px 10px 4px rgba(218, 231, 245, 0.5);

}

.input-box {
    padding: 20px 0;
    box-sizing: border-box;
    border-bottom: 1px solid rgba(239, 239, 239, 1);

    .input-label {
        font-size: 26px;
        margin: 5px 0;
        color: rgba(51, 51, 51, 1);
    }

    .input-fill {
        input {
            width: 100%;
            font-size: 28px;
            padding: 0;
            border: none;
        }

        input::placeholder {
            color: #a8abb2;
        }

        input:disabled {
            // cursor: default;
            background-color: unset !important;
            color: #a8abb2;
        }

    }
}

.btn-list {
    display: flex;
    justify-content: flex-end;
    margin: 20px 0 40px 0;

    .cancel {
        width: 144px;
        height: 76px;
        opacity: 1;
        border-radius: 50px;
        background: rgba(255, 255, 255, 1);
        border: 2px solid rgba(221, 221, 221, 1);
        font-size: 28px;
        color: rgba(166, 166, 166, 1);
    }

    .confirm {
        margin-left: 24px;
        width: 200px;
        height: 76px;
        border-radius: 50px;
        background: linear-gradient(160.78deg, rgba(0, 160, 252, 1) 0%, rgba(8, 173, 255, 1) 100%), linear-gradient(90deg, rgba(47, 135, 229, 1) 0%, rgba(97, 184, 246, 1) 100%);
        font-size: 28px;
        color: rgba(255, 255, 255, 1);
    }

    .btn {
        display: flex;
        align-items: center;
        justify-content: center;
    }
}
</style>